package org.forum.service.impl;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.struts2.ServletActionContext;
import org.forum.dao.MainTopicDao;
import org.forum.dao.UserDao;
import org.forum.domain.Forum;
import org.forum.domain.MainTopic;
import org.forum.domain.User;
import org.forum.service.PersonalHomepageService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PersonalHomepageServiceImpl implements PersonalHomepageService{
	
	Logger logger = LoggerFactory.getLogger(PersonalHomepageServiceImpl.class);
	
	UserDao<User> userDao = null;
	
	MainTopicDao mainTopicDao = null;

	@Override
	public List<Forum> getUserFollowedForums() {
		User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");
		userDao.updateUser(user);
		Set<Forum> forums = user.getForums();
		return setToList(forums);
	}


	@Override
	public List<MainTopic> getMainTopicsByFourms(List<Forum> forums, int pageN_personalHomepage, int pageSize, String sortBy) {
		return mainTopicDao.getMainTopicsByForumIds(forums, pageN_personalHomepage, pageSize, sortBy);
	}
		
	
		
	public List<Forum> setToList(Set<Forum> forums){
		Iterator<Forum> iterator = forums.iterator();
		List<Forum> forums_list = new ArrayList<>();
		while(iterator.hasNext()){
			forums_list.add((Forum)iterator.next());
		}
		return forums_list;
	}

	public void setUserDao(UserDao<User> userDao) {
		this.userDao = userDao;
	}

	public void setMainTopicDao(MainTopicDao mainTopicDao) {
		this.mainTopicDao = mainTopicDao;
	}
	
	
}
